package magicArt.util.db;

import magicArt.util.BusinessConstants.DBOperation;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * DB操作类
 */
public class DBHelper extends SQLiteOpenHelper {

	/** 数据库版本 */
	protected static int VERSION = 1;
	/** db对象 */
	private static SQLiteDatabase db = null;

	/**
	 * 创建DB操作对象
	 * 
	 * @param context
	 * @param operate
	 * @param db
	 */
	public static void CreateDBHelper(Context context, int operate,
			String dbName) {
		switch (operate) {
		// 创建数据库
		case DBOperation.CREATE:
			db = new DBHelper(context, dbName, VERSION).getReadableDatabase();
			break;
		// 查询数据库
		case DBOperation.QUERY:
			db = new DBHelper(context, dbName, VERSION).getReadableDatabase();
			break;
		// 升级数据库
		case DBOperation.UPGRADE:
			db = new DBHelper(context, dbName, VERSION).getReadableDatabase();
			break;
		// 更新数据库
		case DBOperation.UPDATE:
			db = new DBHelper(context, dbName, VERSION).getWritableDatabase();
			break;
		// 插入数据库
		case DBOperation.INSERT:
			db = new DBHelper(context, dbName, VERSION).getWritableDatabase();
			break;
		}
	}

	// 在SQLiteOpenHelper子类当中，必须有该构造函数
	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		// 必须通过super调用父类当中的构造函数
		super(context, name, factory, version);
	}

	// 简化的构造函数
	public DBHelper(Context context, String name, int version) {
		this(context, name, null, version);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	/**
	 * 创建数据库
	 * 
	 * @param sql
	 *            SQL语句
	 */
	public static void create(String sql) {

		if (db != null) {
			try {
				db.execSQL(sql);
			} catch (SQLException e) {
				if (db.isOpen()) {
					db.close();
				}
			}
		}
	}

	/**
	 * 插入记录
	 * 
	 * @param table
	 *            表名
	 * @param values
	 *            记录
	 * @return long
	 */
	public static long insert(String table, ContentValues values) {
		long result = -1;
		if (db != null) {
			result = db.insert(table, null, values);
		}
		return result;
	}

	/**
	 * 删除记录
	 * 
	 * @param table
	 *            表名
	 * @param whereClause
	 *            where语句
	 * @param whereArgs
	 *            参数
	 * @return int
	 */
	public static int delete(String table, String whereClause,
			String[] whereArgs) {
		int result = -1;
		if (db != null) {
			result = db.delete(table, whereClause, whereArgs);
		}
		return result;
	}

	/**
	 * 更新记录
	 * 
	 * @param table
	 *            表名
	 * @param values
	 *            新记录
	 * @param whereClause
	 *            where语句
	 * @param whereArgs
	 *            参数
	 * @return int
	 */
	public static int update(String table, ContentValues values,
			String whereClause, String[] whereArgs) {
		int result = -1;
		if (db != null) {
			result = db.update(table, values, whereClause, whereArgs);
		}
		return result;
	}

	/**
	 * 查询记录
	 * 
	 * @param distinct
	 *            去重
	 * @param table
	 *            表名
	 * @param columns
	 *            查询项目
	 * @param whereClause
	 *            where语句
	 * @param whereArgs
	 *            参数
	 * @param groupBy
	 * @param having
	 * @param orderBy
	 * @param limit
	 *            限定
	 * @return Cursor
	 */
	public static Cursor query(boolean distinct, String table,
			String[] columns, String whereClause, String[] whereArgs,
			String groupBy, String having, String orderBy, String limit) {
		Cursor cursor = null;
		if (db != null) {
			if (limit != null) {
				if (distinct) {
					cursor = db.query(distinct, table, columns, whereClause,
							whereArgs, groupBy, having, orderBy, limit);
				} else {
					cursor = db.query(table, columns, whereClause, whereArgs,
							groupBy, having, orderBy, limit);
				}
			} else {
				cursor = db.query(table, columns, whereClause, whereArgs,
						groupBy, having, orderBy);
			}
		}
		return cursor;
	}

}